home *** CD-ROM | disk | FTP | other *** search
- /*
- xlib.c
- mac hack at x window lib
- oct91
- */
-
- #pragma segment XLib
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- #include <string.h>
- #include <console.h>
- #include <Palettes.h>
-
- #include "glassert.h"
- #include "xlib.h"
-
-
- extern void exitcheck (void);
- static void SetPortPenColors (Display *dsp,
- RGBColor *pRGBForeColor, RGBColor *pRGBBackColor);
- static void CopyBitsPrep (Display *dsp, char fSave);
-
-
- #define True 1
- #define False 0
-
- typedef unsigned short u_short;
- typedef unsigned long u_long;
-
-
- static WindowPtr cw = (WindowPtr) NULL;
-
-
- XImage *XCreateImage(
- Display *dsp,
- Visual *vis,
- unsigned int depth,
- int format,
- int offset,
- char *data,
- unsigned int width,
- unsigned int height,
- int bitmap_pad,
- int bytes_per_line)
- {
- XImage *xim;
- long rowbytes;
- unsigned int hi1 = height;
-
-
- // -- this is how we made data ptr
- //datasize = bpsl * im->h;
- //data = (u_char *) malloc(datasize);
- //bytes_per_line == bpsl
- //heigth == im->h
-
- // fix for Mac bitmap -- bytes_per_line must be word-aligned (2-byte)
- rowbytes = bytes_per_line;
- if (bytes_per_line & 1) {
- char *tbuf;
- unsigned long i;
-
- rowbytes= bytes_per_line+1;
- tbuf = (char*) NewPtrClear( (size_t) rowbytes*hi1 + 4);
- assert (tbuf);
-
- if ((u_long)tbuf & 3 !=0)
- tbuf = (char *) (((u_long)tbuf & 0xfffffffc) + 4); // make it long-aligned
-
- for (i=0; i<hi1; i++)
- memcpy( tbuf +(i*rowbytes), data +(i*bytes_per_line), bytes_per_line);
- free(data);
- data = tbuf;
- }
-
- xim = (XImage *) malloc((size_t) sizeof(XImage));
- assert (xim);
- //use vis to set rgb, ZPixmap stuff
- xim->width = width;
- xim->height = height;
- xim->format = format;
- xim->data = data;
- xim->depth = depth;
- xim->xoffset = offset;
- xim->bitmap_pad = bitmap_pad;
- xim->bytes_per_line = rowbytes; //was bytes_per_line; << fix for mac
- return xim;
- }
-
- void XPutImage(
- Display *dsp,
- Drawable win,
- GC gc,
- XImage *image,
- int src_x,
- int src_y,
- int dest_x,
- int dest_y,
- unsigned int width,
- unsigned int height)
- {
- // display image
-
- // !! this is BAD, win is not &variable, value is not returned !!!
- // win = image->data;
- }
-
-
- #define UseColorWin 1
-
- static PixMapHandle srcpixmap, winpixmap;
- static CTabHandle theColorTab = (CTabHandle) NULL;
- static CTabHandle theDefaultColorTab = (CTabHandle) NULL;
-
-
- void XCopyArea(
- Display *dsp,
- Drawable src,
- Drawable dest,
- GC gc,
- int src_x,
- int src_y,
- unsigned int width,
- unsigned int height,
- int dest_x,
- int dest_y)
- {
- #define cw ((WindowPtr)dsp)
- #define xim ((XImage *)src)
- extern XRectangle window;
- Rect sr, dr;
- int i;
- int dx;
- int dy;
-
-
- if (src==NULL) return;
-
- if (src_x < 0)
- {
- dest_x -= src_x;
- width += src_x;
- src_x = 0;
- }
- if (src_y < 0)
- {
- dest_y -= src_y;
- height += src_y;
- src_y = 0;
- }
-
- if (src_x + width > xim->width)
- width = xim->width - src_x;
- if (src_y + height > xim->height)
- height = xim->height - src_y;
-
- if (height > xim->height)
- height = xim->height;
- if (width > xim->width)
- width = xim->width;
-
- if (width < 0 || height < 0)
- return;
-
- if (src_x > xim->width || src_y > xim->height)
- return;
-
- dx = dest_x;
- dy = dest_y;
- dx += (cw->portRect).left;
- dy += (cw->portRect).top;
-
- SetRect( &dr, dx, dy, dx + width, dy + height);
- SetRect( &sr, src_x, src_y, src_x + width, src_y + height);
-
- #ifdef UseColorWin
- #define db ((BitMap*) (*winpixmap))
- #define sp ((PixMapPtr) (*srcpixmap))
- #define sb ((BitMap*) (*srcpixmap))
- #define pixmapFlag 0x8000
-
- sp->pixelSize = xim->depth;
- sp->cmpSize = sp->pixelSize;
- sp->baseAddr = (Ptr) xim->data;
- sp->rowBytes = xim->bytes_per_line;
- if (sp->pixelSize > 1 && theColorTab /* != (CTabHandle) NULL */)
- {
- CopyBitsPrep (dsp, True);
- sp->pmTable = theColorTab;
- sp->rowBytes |= pixmapFlag;
- }
- SetRect( &sp->bounds, 0, 0, xim->width, xim->height);
-
- CopyBits( sb, db, &sr, &dr, srcCopy, NULL);
-
- if (sp->pixelSize > 1)
- CopyBitsPrep (dsp, False);
-
- #else
- #define db (&(cw->portBits))
- {
- BitMap* sb = (BitMap*) NewPtr(sizeof(BitMap));
-
- sb->baseAddr = (Ptr) xim->data;
- sb->rowBytes = xim->bytes_per_line;
- SetRect( &sb->bounds, 0, 0, xim->width, xim->height);
- CopyBits( sb, db, &sr, &dr, srcCopy, NULL);
- DisposPtr((Ptr) sb);
- }
- #endif
-
- #undef cw
- #undef xim
- #undef db
- #undef sb
- #undef sp
- }
-
-
- Pixmap XCreatePixmap(
- Display *dsp,
- Drawable win,
- unsigned int width,
- unsigned int height,
- unsigned int depth)
- {
- //Pixmap *pix = NewPtr(sizeof(Pixmap));
- //return *pix;
- return NULL;
- }
-
-
- void XFreePixmap(
- Display *dsp,
- Pixmap pix)
- {
- // DisposPtr((ptr)pix);
- }
-
-
- Display *XOpenDisplay( char* name)
- {
- Rect bounds;
- const Bool Visible = True;
- Str255 fName;
-
-
- assert (name);
-
- strcpy ((char *) fName, name);
- SetRect(&bounds, 2, 40, 322, 240);
- #ifdef UseColorWin
- cw= NewCWindow( NULL, &bounds, CtoPstr (fName), Visible, 0, NULL, False, 0);
- srcpixmap = NewPixMap();
- winpixmap = ((CWindowPtr) cw)->portPixMap;
- CopyPixMap( winpixmap, srcpixmap);
- theColorTab = (**winpixmap).pmTable; // set default
- (**theColorTab).ctSeed = GetCTSeed ();
-
- if (theDefaultColorTab == (CTabHandle) NULL &&
- (theDefaultColorTab = theColorTab) /* != (CTabHandle) NULL */)
- HandToHand (&theDefaultColorTab);
- #else
- cw= newwindow( NULL, &bounds, name, Visible, 0, NULL, False, 0);
- #endif
-
- SelectWindow( cw);
- SetPort(cw);
- MoveTo( 9, 40); DrawString("\pWorking...");
- MoveTo( 9, 90); DrawString("\p(command-period to halt)");
-
- return ((Display*) cw);
- }
-
-
- void
- XCloseDisplay (Display *dpy)
-
- {
- if (dpy /* != (Display *) NULL */ &&
- theDefaultColorTab /* != (CTabHandle) NULL */)
- {
- WindowPtr pWindow = (WindowPtr) dpy;
- PixMapHandle hWinPixMap = ((CWindowPtr) pWindow)->portPixMap;
-
-
- (*theDefaultColorTab)->ctSeed = GetCTSeed ();
- (*hWinPixMap)->pmTable = theDefaultColorTab; // set default
-
- CopyBitsPrep (dpy, TRUE);
-
- SelectWindow (pWindow);
- ActivatePalette (pWindow);
-
- (void) usleep (0L);
- }
- }
-
-
- XResizeWindow( dsp, win, width, height)
- Display *dsp;
- Window win;
- unsigned int width, height;
- {
- SizeWindow( (WindowPtr) dsp, width, height, True);
- }
-
-
-
- #define MAXPALCOLS 256
-
- Colormap XCreateColormap(
- Display *dsp,
- Window win,
- Visual *vis,
- int allocflag)
- {
- CTabHandle cmap; // CSpecArray == ColorSpec[]
- #define ctab (**cmap).ctTable
- int i;
- int acolor;
- const int black = 0;
- const int white = 0xffff;
-
- cmap = (CTabHandle) NewHandle(sizeof(ColorTable) + MAXPALCOLS*sizeof(ColorSpec));
- (**cmap).ctSeed = GetCTSeed();
- (**cmap).ctFlags = 0x0000;
- (**cmap).ctSize = MAXPALCOLS-1;
- acolor = white;
- for (i=0;i<MAXPALCOLS; i++) {
- ctab[i].value = i;
- ctab[i].rgb.red = ctab[i].rgb.green = ctab[i].rgb.blue = acolor;
- acolor = black;
- }
- return (Colormap) cmap;
- #undef ctab
- }
-
- void XStoreColors(
- Display *dsp,
- Colormap cmap,
- XColor color[],
- int ncolors)
- {
- #define ctab (**(CTabHandle)cmap).ctTable
- int i;
- Bool ciused[MAXPALCOLS];
- Bool done;
-
-
- assert (cmap);
- if (ncolors /* > 0 */)
- assert (color);
-
- for (i=0; i<MAXPALCOLS; i++) ciused[i]=False;
- (**(CTabHandle)cmap).ctSeed = GetCTSeed(); // notice that we have new colors???
- if (ncolors>MAXPALCOLS) ncolors=MAXPALCOLS;
- for (i=0; i<ncolors; i++) {
- ctab[i].rgb.red = color[i].red;
- ctab[i].rgb.green = color[i].green;
- ctab[i].rgb.blue = color[i].blue;
- }
-
- /*******
- // need to set indices to Device/Std CLUT so we aren't totally mangling
- // out-of-window colors on SetColormap...
- // Can we assume this is called when Std CLUT is in place ?? -- probably not
- if (RealColor(&ctab[i].rgb)) {
- short cind = (short) Color2Index(&ctab[i].rgb);
- ctab[i].value = cind;
- if (cind < MAXPALCOLS) ciused[cind] = True;
- //color[i].pixel = cind;
- }
- else {
- ctab[i].value = MAXPALCOLS; // ?? key we need to force clut ?
- }
- *******/
- /*********
- for (i=0; i<ncolors; i++) {
- if (ctab[i].value == MAXPALCOLS) {
- int j;
- Bool done;
- for (j=0, done=False; (!done) && (j<MAXPALCOLS); j++) {
- ctab[i].value = i;
- ciused[j] = True;
- done=True;
- }
- if (!done)
- ctab[i].value = (short) Color2Index(&ctab[i].rgb);
- }
- }
- ************/
-
- #undef ctab
- }
-
-
-
- void XSetWindowColormap(
- Display *dsp,
- Drawable win,
- Colormap cmap)
- {
- #ifdef UseColorWin
-
- CTabHandle devCmap;
- int i;
- Bool ciused[MAXPALCOLS];
- Bool cineed[MAXPALCOLS];
- #define ctab (**(CTabHandle)cmap).ctTable
- #define dtab (**(CTabHandle)devCmap).ctTable
-
-
- assert (cmap);
-
- theColorTab = (CTabHandle)cmap;
- (**winpixmap).pmTable = theColorTab;
-
- // !need this when ColorTab includes colors not in standard Color Lookup Table
- HLock((Handle)theColorTab);
- SetEntries( 0, (**theColorTab).ctSize, (**theColorTab).ctTable);
- HUnlock((Handle)theColorTab);
-
- // use by-index installation (-1) in Device CLUT, so we preserve out-of-window colors
- // as much as possible ?????
- // SetEntries( -1, (**theColorTab).ctSize, (**theColorTab).ctTable);
-
- /***********
- // identify colors in current CLUT (ciused) & those not in (cineed)
- for (i=0; i<MAXPALCOLS; i++) ciused[i]= False;
- for (i=0; i<<=(**theColorTab).ctSize; i++) cineed[i]= False;
- for (i=0; i<=(**theColorTab).ctSize; i++)
- if (RealColor(&ctab[i].rgb)) {
- short cind = (short) Color2Index(&ctab[i].rgb);
- ciused[cind]= True;
- }
- else {
- cineed[i]= True;
- }
-
- // copy current CLUT
- devCmap = (CTabHandle) NewHandle(sizeof(ColorTable) + MAXPALCOLS*sizeof(ColorSpec));
- (**devCmap).ctSize = MAXPALCOLS-1;
- (**devCmap).ctSeed = GetCTSeed();
- (**devCmap).ctFlags = 0x0000;
- // ?? use this --
- for (i=0; i<MAXPALCOLS; i++) { Index2Color( i, &dtab[i].rgb); dtab[i].value=i; }
-
- // replace entries in current CLUT w/ new colors that are needed
- for (i=0; i<=(**theColorTab).ctSize; i++)
- if (cineed[i]) {
- int j;
- Bool done;
- for (j=0, done=False; (!done) && (j<MAXPALCOLS); j++)
- if (!ciused[j]) {
- dtab[j].rgb = ctab[i].rgb;
- done= True;
- }
- }
-
- // store revised CLUT
- HLock((Handle)devCmap);
- SetEntries( 0, (**devCmap).ctSize, (**devCmap).ctTable);
- HUnlock((Handle)devCmap);
- DisposHandle((Handle)devCmap);
- *********/
-
- #undef ctab
- #undef dtab
-
- #endif
- }
-
- int XAllocColorCells(
- Display *dsp,
- Colormap cmap,
- Bool contig,
- unsigned long plane_masks_return[],
- unsigned int nplanes,
- unsigned long pixels_return[],
- unsigned int npixels)
- {
- return 0;
- }
-
-
- void XSetForeground(
- Display *dsp,
- GC gc,
- unsigned long foreground)
- {
- WindowPtr aPort;
-
-
- GetPort (&aPort);
- SetPort ((WindowPtr) dsp);
-
- #if 0
- if (theColorTab /* != (CTabHandle) NULL */)
- {
- if (--foreground <= (*theColorTab)->ctSize)
- {
- RGBColor *pColor = &((*theColorTab)->ctTable[foreground].rgb);
-
-
- SetPortPenColors (dsp, pColor, (RGBColor *) NULL);
- }
- }
- #else
- PmForeColor (foreground - 1);
- #endif
-
- SetPort (aPort);
- }
-
- void XSetBackground(
- Display *dsp,
- GC gc,
- unsigned long background)
- {
- WindowPtr aPort;
-
-
- GetPort (&aPort);
- SetPort ((WindowPtr) dsp);
-
- #if 0
- if (theColorTab /* != (CTabHandle) NULL */)
- {
- if (--background <= (*theColorTab)->ctSize)
- {
- GrafPtr oldPort;
- RGBColor *pColor = &((*theColorTab)->ctTable[background].rgb);
-
-
- SetPortPenColors (dsp, (RGBColor *) NULL, pColor);
- }
- }
- #else
- PmBackColor (background - 1);
- #endif
-
- SetPort (aPort);
- }
-
-
- static Pixmap stipplePixmap = (Pixmap) NULL;
- static int stippleStyle = FillSolid;
- static int stippleXOrigin = 0;
- static int stippleYOrigin = 0;
-
-
- void
- XSetFillStyle (Display *dsp, GC gc, int fill_style)
-
- {
- stippleStyle = fill_style;
- }
-
-
- void
- XSetStipple (Display *dsp, GC gc, Pixmap stipple)
-
- {
- if (stipple /* != (Pixmap) NULL */)
- stipplePixmap = stipple;
- else
- stippleStyle = FillSolid;
- }
-
-
- void
- XSetTSOrigin (Display *dsp, GC gc, int ts_x_origin, int ts_y_origin)
-
- {
- stippleXOrigin = ts_x_origin;
- stippleYOrigin = ts_y_origin;
- }
-
-
- void XFillRectangle(
- Display *dsp,
- Drawable win,
- GC gc,
- int x,
- int y,
- int width,
- int height)
- {
- WindowPtr aPort;
- Rect dr;
-
- GetPort (&aPort);
- SetPort ((WindowPtr) dsp);
-
- SetRect( &dr, x,y,x+width,y+height);
-
- if (stippleStyle == FillStippled && stipplePixmap /* != (Pixmap) NULL */)
- {
- }
- else
- {
- #if 1
- PaintRect (&dr);
- #else
- FillRect (&dr, white);
- #endif
- }
-
- SetPort (aPort);
- }
-
-
- void XFillRectangles(
- Display *dsp,
- Drawable win,
- GC gc,
- XRectangle *rects,
- int nrects)
- {
- #define setxrects(ar, r) \
- SetRect( &ar, (r).x, (r).y, (r).x+(r).width, (r).y+(r).height)
-
- WindowPtr aPort;
- Rect aRec;
- int i;
-
-
- if (nrects /* > 0 */)
- assert (rects);
-
- GetPort( &aPort);
- SetPort((WindowPtr)dsp);
- for (i=0; i<nrects; i++) {
- setxrects(aRec, rects[i]);
- PaintRect(&aRec);
- }
- SetPort(aPort);
- }
-
-
- void
- XDrawLine (Display *dsp, Drawable win, GC gc, int x1, int y1, int x2, int y2)
-
- {
- WindowPtr aPort;
-
-
- GetPort (&aPort);
- SetPort ((WindowPtr) dsp);
- MoveTo (x1, y1);
- LineTo (x2, y2);
- SetPort (aPort);
- }
-
-
- void
- XDrawPoint (Display *dsp, Drawable win, GC gc, int x, int y)
-
- {
- WindowPtr aPort;
-
-
- GetPort (&aPort);
- SetPort ((WindowPtr) dsp);
- MoveTo (x, y);
- LineTo (x, y);
- SetPort (aPort);
- }
-
-
- Bool StopKey ()
- {
- EventRecord ev;
-
-
- if (EventAvail (keyDownMask | autoKeyMask, &ev))
- {
- if ((ev.modifiers & cmdKey) &&
- ((char) (ev.message & charCodeMask) == '.'))
- {
- SysBeep (1);
- (void) GetNextEvent (keyDownMask |autoKeyMask, &ev);
- return True;
- }
- }
-
- return False;
- }
-
-
- static Bool
- isPressed (unsigned short k) // k = any keyboard scan code, 0-127
-
- {
- unsigned char km[16];
-
-
- GetKeys ((long *) km);
- return ((km[k >> 3] >> (k & 0x07)) & 0x01);
- }
-
-
- Bool cmdKeyIsDown (void)
- {
- return isPressed (55);
- }
-
-
- Bool shiftKeyIsDown (void)
- {
- return isPressed (56);
- }
-
-
- Bool capsLockIsDown (void)
- {
- return isPressed (57);
- }
-
-
- Bool optionKeyIsDown (void)
- {
- return isPressed (58);
- }
-
-
- Bool MouseButton (void)
- {
- EventRecord ev;
- Bool retVal = EventAvail (mouseUp, &ev);
-
-
- if (retVal)
- FlushEvents (everyEvent, 0);
-
- return retVal;
- }
-
-
- Bool
- Keypress (int *pKeypressed)
-
- {
- EventRecord ev;
- Bool status = EventAvail (keyDownMask | keyUpMask | autoKeyMask, &ev);
-
-
- if (status)
- {
- exitcheck ();
-
- *pKeypressed = ev.message & charCodeMask;
- FlushEvents (everyEvent, 0);
- }
-
- return status;
- }
-
-
- char *StdGetFile(
- char* prompt,
- OSType fileTypes[],
- int nFileTypes)
- {
- Point wher; /*where to display dialog*/
- SFReply reply; /*reply record*/
- short len;
- static char filename[80] = "\0";
-
- wher.h = 80;
- wher.v = 90;
- if (optionKeyIsDown()) nFileTypes=0;
-
- SFGetFile(wher, prompt, nil, nFileTypes, fileTypes, nil, &reply);
-
- if (reply.good) {
- len = SetVol(nil, reply.vRefNum);
- len = reply.fName[0];
- strncpy(filename, (char *)(&reply.fName[1]), len);
- filename[len]= '\0';
- return filename;
- }
- else
- return NULL;
- }
-
-
- // usleep(10000); == sleep for 1/100th of a second
-
- int usleep( unsigned long usec)
- {
- long finalTicks = 0;
-
-
- usec >>= 14; // usec /= 16667;
-
- Delay ((usec < 1) ? 1 : usec, &finalTicks);
-
- return 0;
- }
-
- long hundredthsofseconds()
- {
- //return (long) (100 * TickCount() / 60); /* == 100ths of seconds since startup */
- return TickCount() << 1; /* == 60ths of seconds since startup */
- }
-
-
- static short alog2 (register short x);
-
-
- void
- XExit (int status)
-
- {
- short nColorEntries = (*theColorTab)->ctSize;
- short nColorDepth = alog2 (nColorEntries);
- CTabHandle hColorTab = (CTabHandle) GetResource ('clut', 4000 + nColorDepth);
-
-
- FlushEvents (everyEvent, 0);
-
- #if 1
- /*...Restore the default CLUT...*/
-
- if (hColorTab /* != (CTabHandle) NULL */)
- {
- (*hColorTab)->ctSeed = GetCTSeed ();
- (*winpixmap)->pmTable = hColorTab;
- CopyBitsPrep ((Display *) cw, TRUE);
- ShowWindow ((WindowPtr) cw);
- ActivatePalette ((WindowPtr) cw);
- }
- #endif
-
- #if 1
- XCloseDisplay ((Display *) cw);
- #endif
-
- if (status /* != 0 */)
- console_options.pause_atexit = 1;
-
- InitCursor ();
-
- exit (status);
- }
-
-
- static RGBColor colorW = {0xFFFF, 0xFFFF, 0xFFFF};
- static RGBColor colorB = {0x0000, 0x0000, 0x0000};
-
-
- static void
- SetPortPenColors (Display *dsp, RGBColor *pRGBForeColor, RGBColor *pRGBBackColor)
-
- {
- if (dsp /* != (Display *) NULL */)
- {
- GrafPtr oldPort;
- CGrafPtr pPort = (CGrafPtr) dsp;
-
-
- GetPort (&oldPort);
- SetPort ((WindowPtr) dsp);
-
- pPort->txMode = srcCopy;
-
- if (pRGBForeColor /* != (RGBColor *) NULL */)
- #if 1
- pPort->rgbFgColor = *pRGBForeColor;
- #else
- RGBForeColor (pRGBForeColor);
- #endif
-
- if (pRGBBackColor /* != (RGBColor *) NULL */)
- #if 1
- pPort->rgbBkColor = *pRGBBackColor;
- #else
- RGBBackColor (pRGBBackColor);
- #endif
-
- SetPort (oldPort);
- }
- }
-
-
- static void
- CopyBitsPrep (Display *dsp, char fSave)
-
- {
- static RGBColor rgbFore;
- static RGBColor rgbBack;
-
-
- if (fSave)
- {
- CGrafPtr pPort = (CGrafPtr) dsp;
-
-
- rgbFore = pPort->rgbFgColor;
- rgbBack = pPort->rgbBkColor;
-
- SetPortPenColors (dsp, &colorB, &colorW);
- }
- else
- {
- SetPortPenColors (dsp, &rgbBack, &rgbFore);
- }
- }
-
-
- void
- XSetBlackAndWhite (Display *dsp, Drawable win)
-
- {
- if (theColorTab /* != (CTabHandle) NULL */ &&
- (**theColorTab).ctSize /* > 0 */)
- {
- (**theColorTab).ctTable[0].rgb = colorW;
- (**theColorTab).ctTable[1].rgb = colorB;
- }
-
- SetPortPenColors (dsp, &colorB, &colorW);
- }
-
-
- static short
- alog2 (register short x)
-
- {
- register short i = 0;
-
- while (x)
- {
- i++;
- x >>= 1;
- }
-
- return i;
- }
-